/**
 * jims
 */
package com.jims.master.modules.finance.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.service.PatVisitService;
import com.jims.master.modules.finance.entity.InpAcctMaster;
import com.jims.master.modules.finance.entity.InpAcctMoney;
import com.jims.master.modules.finance.entity.PatPrepayment;
import com.jims.master.modules.finance.service.PatPrepaymentService;
import com.jims.master.modules.sys.utils.UserUtils;
import com.thoughtworks.xstream.mapper.Mapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 病人预交金记录Controller
 * @author 赵宁
 * @version 2017-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/finance/patPrepayment")
public class PatPrepaymentController extends BaseController {

	@Autowired
	private PatPrepaymentService patPrepaymentService;
	@Autowired
	private PatVisitService patVisitService;


	@ModelAttribute
	public PatPrepayment get(@RequestParam(required=false) String id) {
		PatPrepayment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = patPrepaymentService.get(id);
		}
		if (entity == null){
			entity = new PatPrepayment();
		}
		return entity;
	}

	/**
	 * 出院结算页面
	 * @param patPrepayment
	 * @param request
	 * @param response
	 * @param model
	 * @return String
	 * @Author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(PatPrepayment patPrepayment, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PatPrepayment> page = patPrepaymentService.findPage(new Page<PatPrepayment>(request, response), patPrepayment); 
		model.addAttribute("page", page);
		return "modules/finance/outHospSettle";
	}


	/**
	 * 预交金缴纳页面
	 * @param request
	 * @param response
	 * @param model
	 * @author 陈涛
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"depositAmountIndex", ""})
	public String depositList(PatPrepayment patPrepayment,String visitId,HttpServletRequest request, HttpServletResponse response, Model model){
		patPrepayment.setOrgId(UserUtils.getOrgId());
		patPrepayment.setVisitId(visitId);
		patPrepayment.setStatus("1");
		Page<PatPrepayment> page = patPrepaymentService.findPage(new Page<PatPrepayment>(request, response), patPrepayment);
		model.addAttribute("page", page);
		model.addAttribute("patPrepayment", patPrepayment);
		return "modules/finance/depositAmount";
	}

	/**
	 * 加载右边列表
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"loadRight", ""})
	public String loadRight(PatVisit patVisit,Model model){
		model.addAttribute("patVisit",patVisit);
		return "modules/finance/depositAmountRight";
	}

	/**
	 * 根据住院号查询病人所有预交金信息
	 * @param model
	 * @param visitNo
	 * @param patPrepayment
	 * @return String
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "getPayments")
	public String getPayments( Model model,String visitNo,PatPrepayment patPrepayment) {
		PatVisit patVisit = patVisitService.findPatInfoByNo(visitNo, UserUtils.getOrgId());
		patPrepayment.setPatVisit(patVisit);
        PatPrepayment patPrepayments= patPrepaymentService.findPrepayments(patPrepayment);
		model.addAttribute("patVisit", patVisit);
		model.addAttribute("patPrepayments",patPrepayments);
		model.addAttribute("user",UserUtils.getUser());
		return "modules/finance/patPaymentsList";
	}

	/**
	 * 缴纳预交金
	 * @param patPrepayment
	 * @param model
	 * @return
	 * @author 陈涛
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(PatPrepayment patPrepayment, Model model) {
		StringData data=new StringData();
		patPrepayment.setAcctNo(null);
		patPrepayment.setOrgId(UserUtils.getOrgId());
		//type  1缴费  2.退费
		int i=patPrepaymentService.savePatPrepayment(patPrepayment);
        if(i>0){
            data.setCode("success");
            data.setData(patPrepayment.getId());
        }else{
			data.setCode("error");
			data.setData("缴纳病人预交金失败");
        }
        return data;
	}

	/**
	 * 预交金作废
	 * @param patPrepayment
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "deletePatPre")
	@ResponseBody
	public StringData deletePatPre(PatPrepayment patPrepayment){
		StringData data=new StringData();
		patPrepayment.setRefundedUser(UserUtils.getUser().getId());
		int i = patPrepaymentService.deletePatPre(patPrepayment);
		if(i>0){
			data.setCode("success");
			data.setData("预交金作废成功！");
		}else{
			data.setCode("error");
			data.setData("预交金作废失败！");
		}
		return data;
	}

	/**
	 * 预交金-退款
	 * @param patPrepayment
	 * @return
	 * @author CTQ
	 * @date 2017-07-20 11:09:06
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "returnPatPre")
	@ResponseBody
	public StringData returnPatPre(PatPrepayment patPrepayment){
		StringData data = patPrepaymentService.returnPatPre(patPrepayment);
		return data;
	}


	/**
	 * 预交金结账页面
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "paymentsSettle")
	public String paymentsSettle(String startTime,String endTime,Model model){
		if(startTime==null || startTime==""){
			startTime= DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
		}
		if(endTime==null || endTime==""){
			endTime=DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
		}
		model.addAttribute("startTime",startTime);
		model.addAttribute("endTime",endTime);
		return "modules/finance/paymentsSettle";
	}

	/**
	 * 预交金结账主记录
	 * @param model
	 * @param startTime
	 * @param endTime
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "settleMoney")
	public String  settleMoney(Model model , String startTime,String endTime,InpAcctMaster inpAcctMaster){
		inpAcctMaster.setCreateBy(UserUtils.getUser());
		inpAcctMaster.setStartDate(DateUtils.parseDate(startTime));
		inpAcctMaster.setEndDate(DateUtils.parseDate(endTime));
		inpAcctMaster.setOrgId(UserUtils.getOrgId());
		inpAcctMaster=patPrepaymentService.getPrepayment(inpAcctMaster);
		model.addAttribute("inpAcctMaster",inpAcctMaster);
		return "modules/finance/paymentsSettleTop";
	}

	/**
	 *查询支付方式以及详细信息
	 * @param model
	 * @param startTime
	 * @param endTime
	 * @param inpAcctMaster
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "settleDetails")
	public String settleDetails(Model model , String startTime,String endTime,InpAcctMaster inpAcctMaster){
		inpAcctMaster.setCreateBy(UserUtils.getUser());
		inpAcctMaster.setStartDate(DateUtils.parseDate(startTime));
		inpAcctMaster.setEndDate(DateUtils.parseDate(endTime));
		inpAcctMaster.setOrgId(UserUtils.getOrgId());
		InpAcctMaster acctMaster=patPrepaymentService.getPrepayment(inpAcctMaster);
        List<InpAcctMoney> inpPaymentsMoneys= patPrepaymentService.getPayWay(inpAcctMaster);
        List<PatPrepayment> patPrepayments=patPrepaymentService.getPaymentsByAcct(inpAcctMaster);
		model.addAttribute("inpAcctMaster",acctMaster);
		model.addAttribute("inpPaymentsMoneys",inpPaymentsMoneys);
		model.addAttribute("patPrepayments",patPrepayments);
		return "modules/finance/paymentsSettleBelow";
	}

	/**
	 * 预交金结账确认
	 * @param startTime
	 * @param endTime
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "confirmPayAcct")
	@ResponseBody
	public StringData confirmPayAcct( String startTime,String endTime,InpAcctMaster inpAcctMaster){
		StringData data=new StringData();
		inpAcctMaster.setCreateBy(UserUtils.getUser());
		inpAcctMaster.setStartDate(DateUtils.parseDate(startTime));
		inpAcctMaster.setEndDate(DateUtils.parseDate(endTime));
		inpAcctMaster.setOrgId(UserUtils.getOrgId());
		String acctNo = patPrepaymentService.confirmAcct(inpAcctMaster);
		if(acctNo!=null && !acctNo.equals("")){
			data.setCode("success");
			data.setData(acctNo);
		}else{
			data.setCode("error");
		}
		return data;
	}

	/**
	 * 预交金结账打印
	 * @param inpAcctMaster
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "printPaymentAcct")
	@ResponseBody
	public StringData printPaymentAcct(InpAcctMaster inpAcctMaster){
		StringData data = new StringData();
		String printPath = patPrepaymentService.prepaymentCheckPrint(inpAcctMaster);
		data.setData(printPath);
		data.setCode("success");
		return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "waitPrepayList")
	public String waitPrepayList(Model model , PatPrepayment patPrepayment){
		patPrepayment.setWardCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
		patPrepayment.updateOrgId();
		if (patPrepayment.getMoney()==null||patPrepayment.getMoney()==0){
			patPrepayment.setMoney(100);
		}
		List<BaseDto> list = patPrepaymentService.searchWaitPrePayment(patPrepayment);
		model.addAttribute("patPrepayment", patPrepayment);
		model.addAttribute("list", list);
		return "modules/nurse/waitPrepayList";
	}
	/**
	 * @param         patPrepayment    传递参数
	 * @return com.jims.common.data.StringData    返回类型
	 * @throws
	 * @Title: reminderPrint
	 * @Description: (预交金催单打印)
	 * @author CTQ
	 * @date 2017-06-13 14:54:09
	 */

	@RequiresPermissions("user")
	@RequestMapping(value = "reminderPrint")
	@ResponseBody
	public StringData reminderPrint(PatPrepayment patPrepayment){
		StringData stringData = new StringData();
		String printPath = patPrepaymentService.reminderPrint(patPrepayment);
		stringData.setCode("success");
		stringData.setData(printPath);
		return stringData;
	}
    /**
     * @param         patPrepayment   传递参数
     * @return com.jims.common.data.StringData    返回类型
     * @throws
     * @Title: print
     * @Description: (预交金打印)
     * @author yangfuda
     * @date 2017-07-20 10:45:09
     */

    @RequiresPermissions("user")
    @RequestMapping(value = "prepaidPaymentPrint")
    @ResponseBody
    public StringData prepaidPaymentPrint(PatPrepayment patPrepayment){
        StringData stringData = new StringData();
        String printPath = patPrepaymentService.prepaidPaymentPrint(patPrepayment);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }

	/**
	 * @param         patPrepayment    传递参数
	 * @return com.jims.common.data.StringData    返回类型
	 * @throws
	 * @Title: detailedListPrint
	 * @Description: (预交金清单打印)
	 * @author CTQ
	 * @date 2017-06-13 14:54:51
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "detailedListPrint")
	@ResponseBody
	public StringData detailedListPrint(PatPrepayment patPrepayment){
		StringData stringData = new StringData();
		String printPath = patPrepaymentService.detailedListPrint(patPrepayment);
		stringData.setCode("success");
		stringData.setData(printPath);
		return stringData;
	}

	/**
	 * 一卡通缴纳预交金
	 * @param patPrepayment
	 * @return
	 * @author CTQ
	 * @date 2017-07-19 19:40:47
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "saveOneCardSolution")
	@ResponseBody
	public StringData saveOneCardSolution(PatPrepayment patPrepayment){
		StringData data=new StringData();

		int i = patPrepaymentService.saveOneCardSolution(patPrepayment);
		if(i>0){
			data.setCode("success");
			data.setData(patPrepayment.getId());
		}else{
			data.setCode("error");
			data.setData("一卡通缴纳预交金失败！");
		}
		return data;
	}


}